<template>
  <div class="picture-manage-home">
    <el-container style="height: 100vh">
      <el-aside width="220px" class="sidebar">
        <div class="sidebar-title">
          <span>无人机回传照片整理程序</span>
        </div>
        <el-menu
          :default-active="activeMenu"
          class="el-menu-vertical-demo"
          @select="handleMenuSelect"
        >
          <el-menu-item index="out-uav">
            <el-icon><Document /></el-icon>
            <span>已出库无人机信息</span>
          </el-menu-item>
        </el-menu>
      </el-aside>
      <el-main class="main-content">
        <div v-if="activeMenu === 'out-uav'">
          <div class="right-top-bar">
            <div class="top-bar-left">
              <span class="breadcrumb">
                <el-breadcrumb separator="/">
                  <el-breadcrumb-item :to="{ path: '/home' }"
                    >首页</el-breadcrumb-item
                  >
                  <el-breadcrumb-item>已出库无人机信息</el-breadcrumb-item>
                </el-breadcrumb>
              </span>
            </div>
            <div class="top-bar-right">
              <span class="user-info">欢迎，{{ username }}</span>
            </div>
          </div>
          <div class="content">
            <div class="page-title-bar">
              <h2 class="page-title">已出库无人机列表</h2>
            </div>
            <el-skeleton
              v-if="loading"
              :loading="loading"
              :rows="6"
              style="padding: 20px"
            />
            <div class="card-list" v-else>
              <div
                class="warehouse-card"
                v-for="item in uavList"
                :key="item.id"
              >
                <div class="card-image">
                  <el-image
                    v-if="item.uavPhoto"
                    :src="getUavImageUrl(item.uavPhoto)"
                    fit="cover"
                    class="warehouse-img"
                  />
                  <div class="placeholder" v-else>
                    <el-icon><Picture /></el-icon>
                    <span>暂无图片</span>
                  </div>
                </div>
                <div class="card-content">
                  <div class="card-header">
                    <h3 class="warehouse-name">{{ item.uavName }}</h3>
                    <span class="stock-tag">
                      <el-icon><Box /></el-icon> 型号：{{ item.uavType }}
                    </span>
                  </div>
                  <p class="location">外观：{{ item.uavExterior }}</p>
                  <p class="location">
                    是否需要维护：{{ item.isMaintenance === "1" ? "是" : "否" }}
                  </p>
                  <p class="location">
                    无人机状态：{{ item.uavStatus === "1" ? "出库" : "未知" }}
                  </p>
                  <p class="location">
                    仓储位置：{{ item.warehouseName || "暂无" }}
                  </p>
                  <div class="card-actions">
                    <el-button
                      type="primary"
                      size="small"
                      @click="viewPhotos(item)"
                      class="view-photos-btn"
                    >
                      <el-icon><Picture /></el-icon>
                      查看照片
                    </el-button>
                  </div>
                </div>
              </div>
              <div class="empty-state" v-if="uavList.length === 0">
                <el-empty description="暂无已出库无人机数据" />
              </div>
            </div>
          </div>
        </div>
      </el-main>
    </el-container>
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import { useRouter } from "vue-router";
import request from "@/utils/request";

const router = useRouter();
const activeMenu = ref("out-uav");
const username = ref("");
const loading = ref(true);
const uavList = ref([]);

function handleMenuSelect(key) {
  activeMenu.value = key;
  if (key === "out-uav") {
    fetchUavList();
  }
}

const viewPhotos = (uav) => {
  router.push({
    path: "/uav-photos",
    query: {
      uavId: uav.id,
      uavName: uav.uavName,
    },
  });
};

const getUavImageUrl = (photoPath) => {
  if (!photoPath) return "";
  try {
    if (photoPath.startsWith("/9j/")) {
      return "data:image/jpeg;base64," + photoPath;
    }
    return photoPath;
  } catch {
    return "";
  }
};

const fetchUavList = async () => {
  try {
    loading.value = true;
    const res = await request.get(
      "/tielingpowergriduavservice/uav/getUavByUavStatus",
      {
        params: { uavStatus: "1" },
      }
    );
    uavList.value = res.data || [];
  } catch (error) {
    console.error("Error fetching UAV list:", error);
    uavList.value = [];
  } finally {
    loading.value = false;
  }
};

onMounted(() => {
  const userInfoStr = localStorage.getItem("userInfo");
  if (userInfoStr) {
    const userInfo = JSON.parse(userInfoStr);
    username.value = userInfo.name;
  }
  if (activeMenu.value === "out-uav") {
    fetchUavList();
  }
});
</script>

<style scoped>
.picture-manage-home {
  width: 100%;
  height: 100%;
  background: #f5f7fa;
  display: flex;
  flex: 1;
}
.sidebar {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-top: 30px;
}
.sidebar-title {
  display: flex;
  align-items: center;
  margin-bottom: 40px;
}
.el-menu-vertical-demo {
  background: transparent;
  border-right: none;
  width: 100%;
}
.el-menu-item {
  color: #fff;
  font-size: 16px;
  height: 48px;
  line-height: 48px;
}
.el-menu-item.is-active {
  background: #199893 !important;
  color: #fff !important;
}
.main-content {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: #f8fafc;
  padding: 0;
}
.right-top-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #fff;
  padding: 0 20px;
  height: 50px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
  z-index: 9;
}
.top-bar-left {
  display: flex;
  align-items: center;
}
.breadcrumb {
  font-size: 14px;
  color: #666;
}
.top-bar-right {
  color: #333;
}
.user-info {
  line-height: 50px;
}
.content {
  flex: 1;
  padding: 30px 50px;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  overflow-x: hidden;
  max-height: calc(100vh - 100px);
}
.page-title-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 30px;
  padding-bottom: 15px;
  border-bottom: 1px solid #e2e8f0;
}
.page-title {
  font-size: 22px;
  font-weight: 600;
  color: #2c3e50;
  margin: 0;
  display: flex;
  align-items: center;
}
.page-title::before {
  content: "";
  display: inline-block;
  width: 4px;
  height: 20px;
  background-color: #3498db;
  margin-right: 10px;
  border-radius: 2px;
}
.card-list {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
  justify-content: flex-start;
}
.warehouse-card {
  width: calc((100% - 80px) / 5); /* 一行5个卡片，减去4个间距 */
  min-width: 200px;
  background-color: #fff;
  border-radius: 10px;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.05);
  overflow: hidden;
  transition: all 0.3s ease;
  cursor: pointer;
  position: relative;
  max-height: 400px;
  display: flex;
  flex-direction: column;
}
.card-image {
  width: 100%;
  height: 160px;
  overflow: hidden;
  position: relative;
}
.warehouse-img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  transition: transform 0.5s ease;
}
.warehouse-card:hover .warehouse-img {
  transform: scale(1.05);
}
.placeholder {
  width: 100%;
  height: 160px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background-color: #f8fafc;
  color: #95a5a6;
  font-size: 14px;
}
.placeholder i {
  font-size: 24px;
  margin-bottom: 8px;
  color: #bdc3c7;
}
.card-content {
  padding: 20px;
}
.card-header {
  display: flex;
  flex-direction: column;
  margin-bottom: 12px;
}
.warehouse-name {
  font-size: 18px;
  font-weight: 500;
  color: #2c3e50;
  margin: 0 0 8px 0;
  line-height: 1.3;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  line-clamp: 1;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.stock-tag {
  display: inline-flex;
  align-items: center;
  padding: 3px 8px;
  font-size: 12px;
  color: #7f8c8d;
  background-color: #f1f5f9;
  border-radius: 4px;
  margin-bottom: 10px;
  width: fit-content;
}
.stock-tag i {
  margin-right: 5px;
  font-size: 12px;
}
.location {
  font-size: 14px;
  color: #7f8c8d;
  margin: 0 0 15px 0;
  line-height: 1.5;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  height: 40px;
}
.card-actions {
  display: flex;
  justify-content: center;
  margin-top: 15px;
}
.view-photos-btn {
  background: linear-gradient(90deg, #199893 0%, #4fd1c5 100%);
  border: none;
  color: #fff;
  font-size: 12px;
  padding: 8px 16px;
  border-radius: 6px;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 4px;
}
.view-photos-btn:hover {
  background: linear-gradient(90deg, #12706b 0%, #199893 100%);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(25, 152, 147, 0.3);
}
.empty-state {
  width: 100%;
  padding: 60px 0;
  display: flex;
  justify-content: center;
}
.photo-list {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
  justify-content: flex-start;
}

/* 响应式优化 */
@media (max-width: 1400px) {
  .warehouse-card {
    width: calc((100% - 60px) / 4); /* 一行4个卡片 */
  }
}

@media (max-width: 1200px) {
  .warehouse-card {
    width: calc((100% - 40px) / 3); /* 一行3个卡片 */
  }
}

@media (max-width: 992px) {
  .warehouse-card {
    width: calc((100% - 20px) / 2); /* 一行2个卡片 */
  }
}

@media (max-width: 768px) {
  .warehouse-card {
    width: 100%; /* 一行1个卡片 */
  }
}
</style>
